<?php
/**
 * The  quiz mistakescollection
 *
 * @package   gradereport_mistakescollection
 * @author Jason <1129332567@qq.com>
 * @copyright  Jason (http://47.114.124.77)
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require('../../../config.php');
require_once ('Page.class.php');

$courseid=required_param('id',PARAM_INT);
$questionid  = required_param('questionid', PARAM_INT);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
require_login($course);
$context = context_course::instance($course->id);
require_capability('moodle/grade:viewall', $context);
$PAGE->set_url(new moodle_url('/grade/report/mistakescollection/detailview.php'),array("id"=>$course->id,'questionid'=>$questionid));
$PAGE->set_pagelayout('report');
$title="试题答错详情";
$nav="<a href='".new moodle_url('/grade/report/mistakescollection/index.php',array("id"=>$courseid))."'>测验错题集</a>&nbsp;&nbsp;/&nbsp;&nbsp;试题答错次数统计&nbsp;&nbsp;/&nbsp;&nbsp;".$title;
$PAGE->navbar->add($nav);
$PAGE->set_title($title);
$PAGE->set_heading($title);

$where="WHERE qas.state IN(:state1,:state2) AND cm.course= :courseid AND qa.questionid= :questionid";

$sql="SELECT count(qas.id) as totalcount
      FROM {question_attempts} qa 
      JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id AND qas.sequencenumber = ( SELECT MAX(sequencenumber) FROM mdl_question_attempt_steps WHERE questionattemptid = qa.id )
      JOIN {question_usages} qu ON qu.id=qa.questionusageid
      JOIN {context} c ON c.id=qu.contextid
      JOIN {course_modules} cm ON cm.id=c.instanceid "
      .$where;

$sql1="SELECT qas.id,q.name as quizname,concat(u.lastname,u.firstname) as username,quiza.id quizattemptid,quiza.attempt,qa.slot,qa.questionsummary,qa.rightanswer,qa.responsesummary, qa.timemodified, qas.id AS attemptstepid, qas.timecreated
      FROM {question_attempts} qa 
      JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id AND qas.sequencenumber = ( SELECT MAX(sequencenumber) FROM mdl_question_attempt_steps WHERE questionattemptid = qa.id )
      JOIN {question_usages} qu ON qu.id=qa.questionusageid
      JOIN {quiz_attempts} quiza ON qu.id=quiza.uniqueid
      JOIN {context} c ON c.id=qu.contextid
      JOIN {course_modules} cm ON cm.id=c.instanceid
      JOIN {quiz} q ON q.id=cm.instance
      JOIN {user} u ON u.id=qas.userid "
      .$where." ORDER BY qa.timemodified DESC ,qa.slot ASC";

$params['state1']="gradedpartial";
$params['state2']="gradedwrong";
$params['courseid']=$course->id;
$params['questionid']=$questionid;

$results = $DB->get_record_sql($sql, $params);
$per=10;
$page=new \gradereport_mistakescollecion\Page($results->totalcount,$per);
$limit=explode(',',$page->limit);
$results = $DB->get_records_sql($sql1, $params,$limit[0],$limit[1]);
$table = new html_table();
$header=array (
    '课程名称',
    "测验名称",
    "用户名",
    "答题次数",
    "题目",
    "你的答案",
    '正确答案',
    "答题时间",
    "题目回顾"
);

$table->head = $header;

foreach ($results as $val) {
    $slotlink="<a href=".new moodle_url('/grade/report/mistakescollection/questionview.php',array('id'=>$courseid,'quizattemptid'=>$val->quizattemptid,'slot'=>$val->slot))." target='_blank' >回顾</a>";
    $questionsummary=shorten_text($val->slot.". ".$val->questionsummary,120);
    $responsesummary=format_text($val->responsesummary,FORMAT_PLAIN);
    $row = array ($course->shortname,$val->quizname,$val->username,"第".$val->attempt."次",$questionsummary,$responsesummary,$val->rightanswer,date('Y-m-d H:i:s',$val->timecreated),$slotlink);
    $table->data[] = $row;
}
//var_dump($table->data);
//exit();
echo $OUTPUT->header();
echo html_writer::start_tag('div', array('class' => 'no-overflow display-table'));
echo html_writer::table($table);
echo html_writer::end_tag('div');
echo "</br>".$page->fpage();
echo $OUTPUT->footer();